package com.makeblock.demo.testdatabinding.list;

import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.makeblock.demo.testdatabinding.R;
import com.makeblock.demo.testdatabinding.databinding.ItemViewBinding;
import com.makeblock.demo.testdatabinding.model.DataModel;
import com.makeblock.demo.testdatabinding.viewmodel.ItemViewModel;

import java.util.List;

/**
 * Created by xuexin on 2016/10/27.
 */

public class RecyclerViewAdapter extends RecyclerView.Adapter<ItemVH> {

    private List<DataModel> list;

    public RecyclerViewAdapter(List<DataModel> list) {
        super();
        this.list = list;
    }

    @Override
    public ItemVH onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemViewBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_view, parent, false);
        ItemVH holder = new ItemVH(binding);
        return holder;
    }

    @Override
    public void onBindViewHolder(ItemVH holder, int position) {
        //貌似这判断没啥用的样子
        if (holder.getBinding().getViewModel() == null) {
            holder.getBinding().setViewModel(new ItemViewModel(list.get(position)));
        } else {
            Log.e("lyh", "不为空,这样应该会耗时稍少一点,待测试");
            if (holder.getBinding().getViewModel().equals(list.get(position))) {
                Log.e("lyh", "应该不会有相等的时候吧");
            } else {
                holder.getBinding().getViewModel().setModel(list.get(position));
            }
        }

    }

    @Override
    public int getItemCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }
}
